package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.fans.request.HistorySendUserRequest;
import com.fulihui.wgj.admin.api.fans.request.SearchFansRequest;
import com.fulihui.wgj.admin.api.fans.vo.FansInfoVO;
import com.fulihui.wgj.admin.api.fans.vo.HistorySendUserVO;
import com.fulihui.wgj.admin.integration.WxUserInfoServiceClient;
import com.fulihui.wgj.facade.dto.WxHistorySendUserDTO;
import com.fulihui.wgj.facade.dto.WxUserInfoDTO;
import com.fulihui.wgj.facade.request.wxuser.WxHistorySendUserQueryRequest;
import com.fulihui.wgj.facade.request.wxuser.WxUserInfoPreviewQueryRequest;
import com.fulihui.wgj.facade.service.wxuser.WxUserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: Levon
 * @date: 2018-07-25 15:15
 */
@Service
public class WxUserInfoServiceClientImpl implements WxUserInfoServiceClient {

    @Reference(version = "1.0.0")
    private WxUserInfoService wxUserInfoService;

    @Override
    public Result<List<HistorySendUserVO>> queryHistorySendFans(HistorySendUserRequest request) {

        WxHistorySendUserQueryRequest queryRequest = new WxHistorySendUserQueryRequest();
        queryRequest.setAppId(request.getAppId());
        SimpleResult<List<WxHistorySendUserDTO>> simpleResult = wxUserInfoService.queryHistorySendUser(queryRequest);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            List<WxHistorySendUserDTO> datas = simpleResult.getData();
            if (!CollectionUtils.isEmpty(datas)) {
                List<HistorySendUserVO> userVOS = BeanConv.copy(datas, HistorySendUserVO.class);
                return JsonResultBuilder.succ(userVOS);
            } else {
                return JsonResultBuilder.succ(null);
            }
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }

    @Override
    public Result<List<FansInfoVO>> searchFans(SearchFansRequest request) {
        WxUserInfoPreviewQueryRequest queryRequest = new WxUserInfoPreviewQueryRequest();
        queryRequest.setNickname(request.getNickname());
        queryRequest.setAppId(request.getAppId());
        SimpleResult<List<WxUserInfoDTO>> simpleResult = wxUserInfoService.queryWxUserPreview(queryRequest);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            List<WxUserInfoDTO> datas = simpleResult.getData();
            List<FansInfoVO> userVOS = null;
            if (!CollectionUtils.isEmpty(datas)) {
                userVOS = BeanConv.copy(datas, FansInfoVO.class);
            }
            return JsonResultBuilder.succ(userVOS);
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }
}
